import time
import pytest
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

class TestAddToCart:
    """测试添加商品到购物车功能"""

    @pytest.mark.order(2)
    def test_add_product_to_cart(self):
        """测试添加商品到购物车流程"""
        # 创建浏览器驱动
        driver = webdriver.Chrome()
        try:
            # 获取访问页面
            driver.get("http://localhost:8080/EasyBuy/Home?action=index")

            # 放大浏览器
            driver.maximize_window()
            time.sleep(2)

            #点击登录
            driver.find_element(By.XPATH,"html/body/div[1]/div/span[2]/span/a[1]").click()
            time.sleep(2)

            #用户名
            driver.find_element(By.XPATH,".//*[@id='loginName']").send_keys("打开2")
            time.sleep(2)

            #密码
            driver.find_element(By.XPATH,".//*[@id='password']").send_keys("xjl021022")
            time.sleep(2)

            #点击登录
            driver.find_element(By.XPATH,"html/body/div[4]/div[2]/div[2]/form/table/tbody/tr[4]/td[2]/input").click()
            time.sleep(2)

            # 实例化并获取ActionChains类
            action = ActionChains(driver)

            #鼠标悬浮
            button = driver.find_element(By.XPATH,"html/body/div[5]/div/div[1]/div[2]/ul/li[1]/div[1]")
            action.move_to_element(button).perform()
            time.sleep(2)

            #点击少女派
            driver.find_element(By.XPATH,"html/body/div[5]/div/div[1]/div[2]/ul/li[1]/div[2]/div/div/a").click()
            time.sleep(2)

            driver.execute_script("window.scrollBy(0, 300);")
            time.sleep(3)

            #点击商品详细页
            driver.find_element(By.XPATH,"html/body/div[6]/div[1]/div[2]/div[2]/ul/li[1]/div[1]/a/img").click()
            time.sleep(2)

            #在新页面继续进行新操作
            driver.switch_to.window(driver.window_handles[-1])
            time.sleep(3)

            driver.execute_script("window.scrollBy(0, 300);")
            time.sleep(3)

            #点击加入购物车
            driver.find_element(By.XPATH,"html/body/div[6]/div[2]/div[2]/div[6]/span/img").click()
            time.sleep(2)

            #取消加入购物车成功的弹出框
            driver.find_element(By.XPATH,".//*[@id='MyDiv1']/div/div[1]/span/img").click()
            time.sleep(2)

            driver.execute_script("window.scrollBy(0, -300);")
            time.sleep(3)

            #点击购物车
            driver.find_element(By.XPATH,".//*[@id='searchBar']/div/div[3]/div[1]").click()
            time.sleep(2)

            #去结算
            driver.find_element(By.XPATH,".//*[@id='searchBar']/div/div[3]/div[2]/div[3]/a").click()
            time.sleep(2)

            #确定结算
            driver.find_element(By.XPATH,".//*[@id='settlement']/div[2]/table/tbody/tr[4]/td/a[2]/img").click()
            time.sleep(2)

            driver.execute_script("window.scrollBy(0, 300);")
            time.sleep(3)

            #勾选新地址
            driver.find_element(By.XPATH,".//*[@id='settlement']/div[2]/div/table[3]/tbody/tr/td[1]/input").click()
            time.sleep(2)

            #填写地址
            driver.find_element(By.XPATH,".//*[@id='settlement']/div[2]/div/table[3]/tbody/tr/td[2]/input[1]").send_keys("东莞市")
            time.sleep(2)

            #确认订单
            driver.find_element(By.XPATH,".//*[@id='settlement']/div[2]/div/table[4]/tbody/tr[2]/td/a/img").click()
            time.sleep(2)
        finally:
            driver.quit()